home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 351-375 / disk_351 / pdc / libsrc.lzh / LibSrc / StdLib / strtosd.c < prev    next >
C/C++ Source or Header  |  1990-04-07  |  2KB  |  81 lines

  1. /*
  2.  * Libraries and headers for PDC release 3.3 (C) 1989 Lionel Hummel.
  3.  * PDC Software Distribution (C) 1989 Lionel Hummel and Paul Petersen.
  4.  * PDC I/O Library (C) 1987 by J.A. Lydiatt.
  5.  *
  6.  * This code is freely redistributable upon the conditions that this 
  7.  * notice remains intact and that modified versions of this file not
  8.  * be included as part of the PDC Software Distribution without the
  9.  * express consent of the copyright holders.  No warrantee of any
  10.  * kind is provided with this code.  For further information, contact:
  11.  *
  12.  *  PDC Software Distribution    Internet:                     BIX:
  13.  *  P.O. Box 4006             or hummel@cs.uiuc.edu            lhummel
  14.  *  Urbana, IL  61801-8801       petersen@uicsrd.csrd.uiuc.edu
  15.  */
  16.  
  17. /* strtosd - parses a scaled double from an ascii string */
  18.  
  19. #include <stddef.h>
  20. #include <ctype.h>
  21.  
  22. /* If (base < 1), strtosd returns the number as being right of the base
  23.  * point.  IE, 123456 is returned as 0.123456.  The only function crazier
  24.  * than this is its unsigned counterpart, strtosud().  It is something of
  25.  * a kludge, designed to simplify the code to collect the fractional part
  26.  * of an FP number.  What else is it good for?  Well, you can always use
  27.  * it to read binary floating point (eg, 01101.1101)!
  28.  */
  29.  
  30. double strtosd(string, ptr, base)
  31. char *string;
  32. char **ptr;
  33. double base;
  34. {
  35.     char *strptr = string;
  36.     double retval = 0.0;
  37.     double sign;
  38.     double obase = base;
  39.     int i = 0;
  40.     char c;
  41.  
  42.     obase = base;
  43.  
  44.     while (isspace(c = *strptr))
  45.         strptr++;
  46.  
  47.     if (c == '-') {
  48.         sign = -1.0;
  49.         c = *(++strptr);
  50.         }
  51.     else
  52.         sign = 1.0;
  53.  
  54.     if (c == '+')
  55.         c = *(++strptr);
  56.  
  57.     while ((c = *strptr) != 0) {
  58.         if (isdigit(c)) {
  59.             i++;
  60.             strptr++;
  61.             if (obase > 1)
  62.                 retval = (retval*base) + (c-'0');
  63.             else {
  64.                 retval += base * (c-'0');
  65.                 base *= obase;
  66.                 }
  67.             }
  68.         else
  69.             break;
  70.         }
  71.  
  72.     if (ptr != NULL) {
  73.         if (i > 0)
  74.             *ptr = strptr;
  75.         else
  76.             *ptr = string;
  77.         }
  78.  
  79.     return(retval * sign);
  80. }
  81.